<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 档口收支
 *
 * @icon fa fa-circle-o
 */
class Ltdangkousz extends Backend
{

    /**
     * Ltdangkousz模型对象
     * @var \app\admin\model\Ltdangkousz
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Ltdangkousz;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            //if($input){}
            $list = $this->model
                //->where("")
                //->where('day <= 5')
                ->order('id','asc')
                ->paginate($limit);

            $input = input();
            if(isset($input['filter']) && !empty(json_decode($input['filter'],true))){
                $filter = json_decode($input['filter'],true);
                if(!empty($filter['day'])) {
                    list($s, $e) = explode(' - ', $filter['day']);
                    $daylist = getDatesBetween($s, $e);
                    //$list = $re;
                    $list = [];
                    foreach ($daylist as $k => $item) {
                        $list[$k]['day'] = $item;
                    }
                }
            }else{
                $new_list = [];
                foreach($list as $k1=>$v1){
                    $day = date('Y-m-').$v1->day;
                    $d = date('Y-m-d',strtotime($day));
                    $new_list[$k1]['day'] = $d;
                }
                $list = $new_list;
            }
            $anzhuang_fee = [];
            $guangmao_arr = [];
            $zhuangji_arr = [];
            $xufei_arr =[];
            $other_arr = [];
            $yiji_arr = [];
            $zhi_za_arr = [];
            $tuimao_arr = [];
            $shou_arr = [];
            $zhi_arr = [];
            $jie_arr = [];
            $dui_arr = [];
            foreach ($list as &$row) {
                $d = $row['day'];
                $ronghe = $this->cacl2($d,'fa_ltronghe','anzhuang_fee');
                $row['ronghe'] = $ronghe;
                $guangmao = $this->cacl2($d,'fa_ltronghe','mao_fee');
                $row['guangmao'] = $guangmao;
                $zhuangji = $this->cacl2($d,'fa_lttiaoshifee');
                $row['zhuangji'] = $zhuangji;
                $xufei = $this->cacl2($d,'fa_ltxufei');
                $row['xufei'] = $xufei;
                $yiji = $this->cacl2($d,'fa_ltyiji');
                $row['yiji'] = $yiji;
                $other = $this->cacl2($d,'fa_ltqitaincome');
                $row['other'] = $other;
                $z = $this->cacl2($d,'fa_ltzafeifee','price',1);
                $row['zhi_za'] = $z;
                $t = $this->cacl2($d,'fa_ltzafeifee','price',2);
                $row['tuimao'] = $t;

                $dui_ronghe = $this->duicacl3($d,'fa_ltronghe','paytype','anzhuang_fee');
                $dui_mao = $this->duicacl3($d,'fa_ltronghe','paytype','mao_fee');
                $dui_xufei = $this->duicacl3($d,'fa_ltxufei','ptype');
                $dui_yiji = $this->duicacl3($d,'fa_ltyiji','ptype');
                $dui_other = $this->duicacl3($d,'fa_ltzafeifee','ptype');

                $duigong = float_two($dui_ronghe+$dui_mao+$dui_xufei+$dui_yiji+$dui_other);
                $row['duigong'] = $duigong;
                $dui_arr[] = $duigong;

                $shou = float_two($ronghe+$guangmao+$xufei+$yiji+$other-$duigong);
                $row['shou'] = $shou;
                $zhi = float_two($z+$t+$zhuangji);
                $row['zhi'] = $zhi;
                $jie = float_two($shou-$zhi);
                $row['jieyu'] = $jie;
                $anzhuang_fee[] = $ronghe;
                $xufei_arr[] =$xufei;
                $guangmao_arr[] = $guangmao;
                $zhuangji_arr[] = $zhuangji;
                $other_arr[] = $other;
                $yiji_arr[] = $yiji;
                $zhi_za_arr[] = $z;
                $tuimao_arr[] = $t;
                $shou_arr[] = $shou;
                $zhi_arr[] = $zhi;
                $jie_arr[] = $jie;
            }

            //$result = array("total" => $list->total(), "rows" => $list->items());
            if(!empty($list)){
                $firstarr[]=[
                    'ronghe'          => '总计: '.float_two(array_sum($anzhuang_fee)),
                    'guangmao'          => '总计: '.float_two(array_sum($guangmao_arr)),
                    'zhuangji'          => '总计: '.float_two(array_sum($zhuangji_arr)),
                    'xufei'          => '总计: '.float_two(array_sum($xufei_arr)),
                    'yiji'          => '总计: '.float_two(array_sum($yiji_arr)),
                    'other'          => '总计: '.float_two(array_sum($other_arr)),
                    'zhi_za'          => '总计: '.float_two(array_sum($zhi_za_arr)),
                    'tuimao'          => '总计: '.float_two(array_sum($tuimao_arr)),
                    'shou'          => '总收入: '.float_two(array_sum($shou_arr)),
                    'zhi'          => '总支出: '.float_two(array_sum($zhi_arr)),
                    'jieyu'          => '结余: '.float_two(array_sum($jie_arr)),
                    'duigong'          => '总计: '.float_two(array_sum($dui_arr)),
                ];
                $list = array_merge($list, $firstarr);
            }
            $result = array("total" => count($list), "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    protected function cacl2($d,$table,$field='price',$type = '')
    {

        if(!empty($type)){
            $tfield = $table == 'fa_dxzfshouru'?'stype':'ztype';
            $ronghe = Db::table($table)
                ->where('opt_date',$d)
                ->where($tfield,$type)
                ->whereNull('deletetime')
                ->sum($field);
        }else{
            $ronghe = Db::table($table)
                ->where('opt_date',$d)
                //->where('memo',$memo)
                ->whereNull('deletetime')
                ->sum($field);
        }
        return $ronghe==0?0:float_two($ronghe);
    }

    protected function duicacl3($d,$table,$pfield='',$field='price')
    {


        $ronghe = Db::table($table)
            ->where('opt_date',$d)
            //->where('memo',$memo)
            ->whereNull('deletetime')
            ->where($pfield,5)
            ->sum($field);

        return $ronghe==0?0:float_two($ronghe);
    }

}
